<template>
  <div class="app-container">
    <div>
      <el-form
        :model="sestuForm"
        ref="sestuForm"
        v-show="showSearch"
        label-width="90px"
      >
        <el-form-item label="请选择考试" prop="radio">
          <!-- <div>
           <el-radio-group v-model="sestuForm.examinationId" @change="gedetail(sestuForm.examinationId)" style="margin:10px 10px; " >
            <el-radio-button v-for="(item,i) in examData"  :key="i" :label="item.id" >{{item.name}}</el-radio-button>
         </el-radio-group>
       </div> -->
          <div class="nav">
            <!-- <div
              v-for="(item, i) in examData"
              :key="i"
              :class="item.changecolor == false ? 'content' : 'contentchange'"
              @click="handleColor(item)"
            >
              {{ item.name }}
            </div> -->
            <el-select v-model="sestuForm.examinationId" filterable placeholder="请选择考试" @change="handleColor">
              <el-option
                v-for="item in examData"
                :key="item.value"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="请选择场次" prop="sessionId">
          <!-- <div>
            <span v-if="grouData==''">请先选择考试</span>
          <el-radio-group v-model="sestuForm.sessionId" style="margin:10px 10px; " @change="getList()">
            <el-radio-button v-for="(item,i) in grouData"  :key="i" :label="item.id">{{item.name}}</el-radio-button>
         </el-radio-group>
       </div> -->
          <div class="nav">
            <div
              v-for="(item, i) in grouData"
              :key="i"
              :class="item.changecolor == false ? 'content' : 'contentchange'"
              @click="beforegetList(item.id)"
            >
              {{ item.name }}
            </div>
            <!-- <div>v-for="(item,i) in listdata"  :key="i" :label="item.id" >{{item.sestuForm.examinationId}}</div> -->
          </div>
        </el-form-item>
        <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
      </el-form>
    </div>
    <!-- 搜索功能留着还有用 -->
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="考试主键id" prop="examinationId">
        <el-input
          v-model="queryParams.examinationId"
          placeholder="请输入考试主键id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考场id" prop="sessionId">
        <el-input
          v-model="queryParams.sessionId"
          placeholder="请输入考场id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷表主键id" prop="paperId">
        <el-input
          v-model="queryParams.paperId"
          placeholder="请输入试卷表主键id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考生状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入考生状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考生总分" prop="totalScore">
        <el-input
          v-model="queryParams.totalScore"
          placeholder="请输入考生总分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(0)"
          >批量引用</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            >批量导入</el-button
          >
        </el-col>
         <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(1)"
          >临时添加考生</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam:examinee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam:examinee:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['exam:examinee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="examineeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column
        label="考生姓名"
        align="center"
        :show-overflow-tooltip="true"
        prop="nickName"
      />
      <el-table-column
        label="考生角色"
        align="center"
        :show-overflow-tooltip="true"
        prop="roleName"
      />
      <el-table-column
        label="考生学号"
        align="center"
        :show-overflow-tooltip="true"
        prop="userName"
      />
      <!-- <el-table-column
        label="考生总分"
        align="center"
        :show-overflow-tooltip="true"
        prop="id"
      />
      <el-table-column
        label="创建人"
        align="center"
        :show-overflow-tooltip="true"
        prop="createBy"
      /> -->
      <!-- <el-table-column
        label="考试主键id"
        align="center"
        :show-overflow-tooltip="true"
        prop="examinationId"
      />
      <el-table-column
        label="创建时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="考场id"
        align="center"
        :show-overflow-tooltip="true"
        prop="sessionId"
      />
      <el-table-column
        label="修改人"
        align="center"
        :show-overflow-tooltip="true"
        prop="updateBy"
      />
      <el-table-column
        label="试卷表主键id"
        align="center"
        :show-overflow-tooltip="true"
        prop="paperId"
      />
      <el-table-column
        label="修改时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      -->
      <!-- <el-table-column
        label="考生状态"
        align="center"
        :show-overflow-tooltip="true"
        prop="state"
      /> -->

      <el-table-column
        label="考生状态"
        align="center"
        :show-overflow-tooltip="true"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color: #363636"
            >未分配试卷</span
          >
          <span v-if="scope.row.state == 1" style="color: #363636"
            >未开始考试</span
          >
          <span v-if="scope.row.state == 2" style="color: #ffd700"
            >正在考试</span
          >
          <span v-if="scope.row.state == 3" style="color: #9370db"
            >尚未批阅</span
          >
          <span v-if="scope.row.state == 4" style="color: #00ff00"
            >批阅完成</span
          >
          <span v-if="scope.row.state == 5" style="color: #ff3030"
            >考试状态异常</span
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        :show-overflow-tooltip="true"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:examinee:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:examinee:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="sestuForm.pageNum"
      :limit.sync="sestuForm.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引入考生对话框 -->

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="90%"
      append-to-body
      :before-close="handleClose"
    >
      <searchstudent :row="row" :flag="flag" v-if="open"></searchstudent>
      <!-- <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考试主键id" prop="examinationId">
          <el-input v-model="form.examinationId" placeholder="请输入考试主键id" />
        </el-form-item>
        <el-form-item label="考场id" prop="sessionId">
          <el-input v-model="form.sessionId" placeholder="请输入考场id" />
        </el-form-item>
        <el-form-item label="试卷表主键id" prop="paperId">
          <el-input v-model="form.paperId" placeholder="请输入试卷表主键id" />
        </el-form-item>
        <el-form-item label="考生状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入考生状态" />
        </el-form-item>
        <el-form-item label="考生总分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入考生总分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="
          upload.url +
          '?updateSupport=' +
          upload.updateSupport +
          '&examId=' +
          this.sestuForm.examinationId +
          '&sessionId=' +
          this.sestuForm.sessionId
        "
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <div v-show="downmsg != ''" style="margin-top: 10px"> -->
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate()"
            >下载模板</el-link
          >
          <!-- </div> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExaminee,
  getExaminee,
  delExaminee,
  addExaminee,
  updateExaminee,
  exportExaminee,
  importTemplate,
} from '@/api/exam/examinee'
import {
  listExam,
  addExam,
  getExam,
  delExame,
  updateExam,
} from '@/api/exam/set'
import { getToken } from '@/utils/auth'
import searchstudent from './searchstudent'
import search from '../../paper/paper/search.vue'
import Search from '../../paper/paper/search.vue'
export default {
  components: { searchstudent },
  name: 'Examinee',
  data() {
    return {
      //传递到下一个界面的数据
      row: [],
      //展开层
      activeNamefirst: '1',
      activeNamesecond: '1',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      examineeIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引入考生表格数据
      examineeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //区分批量引入和临时引入
      flag: 0,
      //查询考生参数
      sestuForm: {
        examinationId: '',
        sessionId: '',
        pageNum: 1,
        pageSize: 10,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/exam/examinee/importData',
      },
      //数组
      listdata: [
        // {
        //   text: "请点一下",
        //   changecolor: false,
        // },
        // {
        //   text: "有些变化",
        //   changecolor: false,
        // },
        // {
        //   text: "什么变化",
        //   changecolor: false,
        // },
        // {
        //   text: "仔细看看",
        //   changecolor: false,
        // },
        // {
        //   text: "啊哈哈哈",
        //   changecolor: false,
        // },
      ],
      //试卷列表
      examData: [],
      //场地列表
      grouData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examinationId: null,
        sessionId: null,
        paperId: null,
        state: null,
        totalScore: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //用户下载的东西
      downmsg: '',
    }
  },
  created() {
    this.getListexam()
  },
  methods: {
    handleColor(item) {
      // for (let i = 0; i < this.examData.length; i++) {
      //   this.examData[i].changecolor = false
      // }
      // for (let i = 0; i < this.grouData.length; i++) {
      //   this.grouData[i].changecolor = false
      // }
      // item.changecolor = !item.changecolor
      // this.sestuForm.examinationId = item.id
      this.gedetail(item)
    },
    /** 查询引入考生列表 */
    getList() {
      this.loading = true
      listExaminee(this.sestuForm).then((response) => {
        this.examineeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 关闭弹出框后重新查询页面信息
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.reset()
          this.getList()
          done()
        })
        .catch((_) => {})
    },
    beforegetList(itemid) {
      for (let i = 0; i < this.grouData.length; i++) {
        if (this.grouData[i].id == itemid) {
          this.grouData[i].changecolor = true
        } else {
          this.grouData[i].changecolor = false
        }
      }
      this.sestuForm.sessionId = itemid
      this.getList()
    },
    //查询考试详细信息，场次
    gedetail(tempid) {
      const id = tempid
      getExam(id).then((response) => {
        this.grouData = response.data.sessionList
        //
        //给数组加判断条件
        for (let i = 0; i < this.grouData.length; i++) {
          if (i == 0) {
            this.grouData[i].changecolor = true
          } else {
            this.grouData[i].changecolor = false
          }
        }
        console.log(this.grouData)
        this.sestuForm.sessionId = this.grouData[0].id
        this.getList()
      })
    },

    //获取考试列表
    getListexam() {
      this.loading = true
      listExam().then((response) => {
        this.examData = response.rows
        this.loading = false
        // 在这里加
        for (let i = 0; i < this.examData.length; i++) {
          if (i == 0) {
            this.examData[i].changecolor = true
          } else {
            this.examData[i].changecolor = false
          }
        }
        this.sestuForm.examinationId = this.examData[0].id
        console.log(this.examData)
        this.gedetail(this.sestuForm.examinationId)
      })
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '考生导入'
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      // this.upload.open = false;
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.downmsg = response.msg
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then((response) => {
        this.download(response.msg)
      })
      // importTemplate().then(response => {
      // this.download(response.msg);
      // this.download(this.downmsg);
      // this.upload.open = false;
      // this.downmsg = "";
      // });
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        examinationId: null,
        createTime: null,
        sessionId: null,
        userId: null,
        updateBy: null,
        paperId: null,
        updateTime: null,
        state: null,
        totalScore: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.examineeIds = selection.map((item) => item.examineeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(flag) {
      this.row = this.sestuForm
      this.open = true
      this.flag=flag
      this.title = '引入考生'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExaminee(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改引入考生'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateExaminee(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addExaminee(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const ids = row.examineeId || this.examineeIds
      this.$confirm(
        '是否确认删除引入考生编号为"' + ids + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delExaminee(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有引入考生数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportExaminee(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>

<style lang="scss" scoped>
.chose {
  background-color: #e4e7ed;
}
.nav {
  display: flex;
  flex-wrap: wrap;
}
.content,
.contentchange {
  display: flex;
  border-radius: 10px;
  margin-bottom: 10px;
  // margin-right: 10px;
  // right: 5px;
  // text-align: center;
  // min-height: 28px;
  // position: relative;
  // display: block;
  padding: 8px 15px;
  // font: 16px/1.5 Arial, "微软雅黑", "Microsoft YaHei", Helvetica,
  //   "Helvetica Neue", Tahoma, Arial, sans-serif;
  // width: fit-content; //div宽度自适应文字内容
  // width: -webkit-fit-content;
  // width: -moz-fit-content;
}
.contentchange {
  margin-right: 10px;
  background-color: #409eff;
  color: #f1f1f1;
}
.content:hover {
  background-color: #f1f1f1;
}
</style>
